package com.hdx.server.service.impl;


import com.hdx.common.constant.StatusConstant;
import com.hdx.common.context.BaseContext;
import com.hdx.model.entity.AddressBook;
import com.hdx.server.mapper.AddressBookMapper;
import com.hdx.server.service.AddressBookService;
import org.apache.poi.ss.formula.functions.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBook> getAllAddressBook() {
        Long userId = BaseContext.getCurrentId();
//        List<AddressBook> addressBookList=addressBookMapper.getAllAddressBook(userId);
        List<AddressBook> addressBookList=addressBookMapper.getAllAddressBook(1L);
        return addressBookList;
    }

    @Override
    public void save(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(1L);
//        addressBook.setUserId(userId);
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBookMapper.insert(addressBook);
    }

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook=addressBookMapper.getById(id);
        return addressBook;
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);
    }

    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    public List<AddressBook> getDefault() {
//        Long userId = BaseContext.getCurrentId();
        Long userId = 1L;

        Integer isDefault=StatusConstant.ENABLE;
        List<AddressBook> defaultAddressBookList=addressBookMapper.getDefault(userId,isDefault);



        return defaultAddressBookList;
    }


}

